//
//  EFFlashViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/6/10.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFFlashViewController.h"
#import "EFTimeSegmentView.h"
#import "EFSaleListItem.h" //每一个time对应的对象
#import "EFFlashPageItem.h"//服务器请求整体的对象
#import "EFSearchResultCell.h"
#import "EFGoodsDetailViewController.h"
#import "EFSepecialItem.h"
#import "EFFlashPageItem.h"
#import "EFUserLocationItem.h"

@interface EFFlashViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UIImageView *topImageV;
@property (strong, nonatomic) EFTimeSegmentView *timeSegmentScrollView;

@property (strong, nonatomic) UIView *timeTipView;
@property (strong, nonatomic) UILabel *rightLabel;
@property (strong, nonatomic) UILabel*leftLabel;

@property (strong, nonatomic) UIImageView *selectImageView;
@property (strong, nonatomic) UILabel *dateSLabel;
@property (strong, nonatomic) UILabel *timeSLabel;

@property (strong, nonatomic) EFFlashPageItem *flashPageItem;

//@property (strong, nonatomic) UIScrollView *contentScrollView;
@property (strong, nonatomic) NSMutableArray *dataArray;//不同的页面下换dataArray展示即可
@property (strong, nonatomic) UITableView *tableView;//每个对应一个

@property (strong, nonatomic) EFFlashPageItem *flashItem;
@property (strong, nonatomic) NSNumber *eventId;
@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) NSInteger size;
@end

@implementation EFFlashViewController

- (instancetype)initWithEventId:(NSNumber *)eventId{
    self = [super init];
    if (self) {
        self.title = @"秒杀";
        self.eventId = eventId;
        self.page = 1;
        self.size = 20;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [[NSMutableArray alloc] init];
    [self.view addSubview:self.topImageV];
    [self.view addSubview:self.timeSegmentScrollView];
    self.timeSegmentScrollView.top = self.topImageV.bottom;
    
    [self.view addSubview:self.timeTipView];
    [self.timeTipView addSubview:self.leftLabel];
    [self.timeTipView addSubview:self.rightLabel];
    self.timeTipView.top = self.timeSegmentScrollView.bottom;
    self.leftLabel.left = 20;
    self.leftLabel.top = (self.timeTipView.height - self.leftLabel.height)/2;
    self.rightLabel.right = kScreenWidth - 16;
    self.rightLabel.top = self.leftLabel.top;
    
    [self.view addSubview:self.tableView];
    self.tableView.top = self.timeTipView.bottom;
    
    [self.view addSubview:self.selectImageView];
    self.selectImageView.top = self.timeSegmentScrollView.top;
    self.selectImageView.left = 0;
    [self.selectImageView addSubview:self.dateSLabel];
    [self.selectImageView addSubview:self.timeSLabel];


    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf.dataArray removeAllObjects];
        weakSelf.page = 1;
        [weakSelf requestContent];
        
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        if (weakSelf.dataArray.count >= weakSelf.flashItem.saleTotal.integerValue) {
            [weakSelf.tableView.footer endRefreshing];
            return ;
        }

        weakSelf.page ++;
        [weakSelf requestContent];
    }];
    [self requestContent];
}

- (void)requestContent{
    __weak typeof(self) weakSelf = self;
    [self showHUD];
    //组装position
    CLLocationCoordinate2D coor = APP_CONTEXT.locationManager.pinLocation.userCoordinate;
    NSString *str = [NSString stringWithFormat:@"%f,%f",coor.longitude,coor.latitude];
    [APP_CONTEXT.net getSaleList:self.eventId position:str page:[NSNumber numberWithInteger:self.page] size:[NSNumber numberWithInteger:self.size] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        weakSelf.HUD.hidden = YES;
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        if (code == 0) {
            weakSelf.flashItem = [EFFlashPageItem yy_modelWithDictionary:info];
            [weakSelf.topImageV setImageWithURL:[NSURL URLWithString:weakSelf.flashItem.bannerImg]];
           
            if (weakSelf.page == 1) {
                weakSelf.dataArray = self.flashItem.list;
            }else{
                [weakSelf.dataArray addObjectsFromArray:self.flashItem.list];
            }
            [weakSelf setObject:self.flashItem.list];
            [weakSelf.tableView reloadData];
        }
    }];
}

- (void)setObject:(NSMutableArray *)itemArray{
    if (itemArray.count > 0) {
        __weak typeof(self) weakSelf = self;
        [self.timeSegmentScrollView setTimeSegmentItems:itemArray selectBlock:^(NSInteger index) {
            if (index <= itemArray.count) {
                //设置绿色图片跑到哪个文字
                //图片跑到点击的位置上
                EFSaleListItem *listItem  = itemArray[index];
                weakSelf.selectImageView.left = index* 75;
                //刷新UI
                weakSelf.dateSLabel.text = listItem.date;
                [weakSelf.dateSLabel sizeToFit];
                weakSelf.timeSLabel.text = listItem.startTime;
                [weakSelf.timeSLabel sizeToFit];
                
                NSArray *specialItem = listItem.productList;
                weakSelf.dataArray = [NSMutableArray arrayWithArray:specialItem];
                [weakSelf.tableView reloadData];
            }
        }];
        //默认展示第一个
        EFSaleListItem *listItem  = itemArray[0];
        NSArray *specialItem = listItem.productList;
        weakSelf.dataArray = [NSMutableArray arrayWithArray:specialItem];
        
        //选中的label在和selectImageView保持一层才能看见
        weakSelf.selectImageView.left = 0;
        self.dateSLabel.text = listItem.date;
        [self.dateSLabel sizeToFit];
        self.timeSLabel.text = listItem.startTime;
        [self.timeSLabel sizeToFit];
        
        self.dateSLabel.top= 3;
        self.dateSLabel.left = (self.selectImageView.width - self.dateSLabel.width)/2;
        
        self.timeSLabel.top = self.dateSLabel.bottom + 5;
        self.timeSLabel.left = (self.selectImageView.width - self.timeSLabel.width)/2;
    }
}


#pragma mark - TableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 102;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    if (tableView == self.tableView) {
        ICTableViewItem *item = self.dataArray[indexPath.row];
        ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"search"];
        if (cell == nil){
            cell = [[EFSearchResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"search"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [(EFSearchResultCell *)cell setObject:(EFSepecialItem *)item];
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // ICTableViewItem *item = self.dataArray[indexPath.row];
   if(tableView == self.tableView){
        EFSepecialItem *item = self.dataArray[indexPath.row];
        //跳转商品详情页
        EFGoodsDetailViewController *vc = [[EFGoodsDetailViewController alloc] initWithProductId:item.id wareHouseId:item.warehouse_id];
        [ICNavigator pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)timeTipView{
    if (!_timeTipView) {
        _timeTipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 25)];
        _timeTipView.backgroundColor = RGBCOLOR(0xd2e9cc);
    }
    return _timeTipView;
}

- (UILabel *)rightLabel{
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.font = [UIFont systemFontOfSize:10];
        _rightLabel.textColor = RGBCOLOR(0x69ba45);
        _rightLabel.text = @"距离结束：";
        [_rightLabel sizeToFit];
    }
    return _rightLabel;
}

- (UILabel *)leftLabel{
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.font = [UIFont systemFontOfSize:10];
        _leftLabel.textColor = RGBCOLOR(0x69ba45);
        _leftLabel.text = @"正在抢购，先下单先得哦！";
        [_leftLabel sizeToFit];
    }
    return _leftLabel;
}

- (UIImageView *)selectImageView{
    if (!_selectImageView) {
        _selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 51)];
        _selectImageView.image = IMG(@"Spike_menu_button_bg_home_selected");
    }
    return _selectImageView;
}

- (UILabel *)dateSLabel{
    if (!_dateSLabel) {
        _dateSLabel = [[UILabel alloc] init];
        _dateSLabel.font = [UIFont systemFontOfSize:13];
        _dateSLabel.textColor = [UIColor whiteColor];
    }
    return _dateSLabel;
}

- (UILabel *)timeSLabel{
    if (!_timeSLabel) {
        _timeSLabel = [[UILabel alloc] init];
        _timeSLabel.font = [UIFont boldSystemFontOfSize:17];
        _timeSLabel.textColor = [UIColor whiteColor];
    }
    return _timeSLabel;
}

- (UIImageView *)topImageV{
    if (!_topImageV) {
        _topImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 164)];
        _topImageV.backgroundColor = kGrayColor;
        _topImageV.contentMode = UIViewContentModeScaleAspectFill;
        _topImageV.clipsToBounds = YES;
    }
    return _topImageV;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - 70.f - 164)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (EFTimeSegmentView *)timeSegmentScrollView{
    if (!_timeSegmentScrollView) {
        _timeSegmentScrollView = [[EFTimeSegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
    }
    return _timeSegmentScrollView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
